home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6173 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.4 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: [Help] I can't find my error.
  5. Date: 22 Feb 96 15:44:38 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.825003878@rscernix>
  8. References: <4ggvgr$1b2@aurora.engr.LaTech.edu>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4ggvgr$1b2@aurora.engr.LaTech.edu> pluu@engr.LaTech.edu (PL) writes:
  13.  
  14. >#include <stdio.h>
  15. >
  16. >main()
  17. >{
  18. >    char name;    
  19.  
  20. This doesn't look like a declaration for an array of characters, does it?
  21.  
  22. >    int  age, next_age; 
  23. >
  24. >    printf("%s\n","Please enter your name and age: ");
  25.  
  26. Why not:
  27.     printf("Please enter your name and age:\n");
  28.  
  29. Or the nicer:
  30.     printf("Please enter your name and age: ");
  31.     fflush(stdout);
  32.  
  33. >    scanf("%s%d\n", &name, &age);
  34.  
  35. Never assume that scanf always succeeds.  Always check its return value,
  36. to see if the user hasn't typed some gibberish.
  37.  
  38. %s in scanf is allowing the user to crash your program, if he types more
  39. characters than you're prepared to accept.  Always use %NNs where NN
  40. stands for the size of the character array which will receive the input
  41. minus one.
  42.  
  43. >    next_age= age + 1;
  44. >    printf("Hi, %s ,next year, you will be %d\n", name, next_age);
  45. >}
  46.  
  47. Dan
  48. --
  49. Dan Pop
  50. CERN, CN Division
  51. Email: danpop@mail.cern.ch 
  52. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  53.